package com.ssy.lingxi.marketing.serviceimpl;

import com.ssy.lingxi.common.constant.marketing.ActivityTypeEnum;
import com.ssy.lingxi.common.constant.marketing.BelongTypeEnum;
import com.ssy.lingxi.common.exception.BusinessException;
import com.ssy.lingxi.common.model.dto.UserLoginCacheDTO;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.marketing.entity.activity.ActivityGoodsDO;
import com.ssy.lingxi.marketing.entity.activity.MerchantActivityDO;
import com.ssy.lingxi.marketing.model.bo.ActivityDefinedBO;
import com.ssy.lingxi.marketing.model.constant.ActivityStrOperateEnum;
import com.ssy.lingxi.marketing.model.constant.CommonBooleanEnum;
import com.ssy.lingxi.marketing.model.constant.MarketingConstants;
import com.ssy.lingxi.marketing.model.constant.MerchantActivityInnerStatusEnum;
import com.ssy.lingxi.marketing.model.vo.activity.request.*;
import com.ssy.lingxi.marketing.model.vo.activity.response.*;
import com.ssy.lingxi.marketing.model.vo.common.request.CommonIdReq;
import com.ssy.lingxi.marketing.model.vo.common.response.TaskStepVO;
import com.ssy.lingxi.marketing.model.vo.coupon.request.FilterSkuIdRequest;
import com.ssy.lingxi.marketing.model.vo.coupon.request.FilterSkuIdResponse;
import com.ssy.lingxi.marketing.repository.ActivityGoodsRepository;
import com.ssy.lingxi.marketing.repository.ActivityMemberLevelRepository;
import com.ssy.lingxi.marketing.repository.ActivityShopRepository;
import com.ssy.lingxi.marketing.repository.MerchantActivityRepository;
import com.ssy.lingxi.marketing.service.IMerchantActivityService;
import com.ssy.lingxi.marketing.service.IProcessFeignService;
import com.ssy.lingxi.marketing.service.IScheduleFeignService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityGoodsService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityRecordService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseActivityRuleService;
import com.ssy.lingxi.marketing.serviceimpl.base.activity.BaseMerchantActivityService;
import com.ssy.lingxi.marketing.serviceimpl.component.SuitableFieldComponent;
import com.ssy.lingxi.marketing.serviceimpl.component.activity.ActivityCheckRuleComponent;
import com.ssy.lingxi.product.api.model.response.commodity.CommodityGoodsResponse;
import com.ssy.lingxi.workflow.api.model.contant.ProcessTaskStatusEnum;
import com.ssy.lingxi.workflow.api.model.vo.request.TaskExecuteVO;
import com.ssy.lingxi.workflow.api.model.vo.response.SimpleTaskCompleteVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 营销能力 - 商家活动服务实现类
 * @author yzc
 * @version 2.0.0
 * @date 2021/8/17
 */
@Slf4j
@Service
public class MerchantActivityServiceImpl extends BaseMerchantActivityService implements IMerchantActivityService {

    @Resource
    private MerchantActivityRepository merchantActivityRepository;
    @Resource
    private IProcessFeignService processFeignService;
    @Resource
    private ActivityMemberLevelRepository memberLevelRepository;
    @Resource
    private ActivityShopRepository shopRepository;
    @Resource
    private ActivityGoodsRepository goodsRepository;
    @Resource
    private ActivityCheckRuleComponent activityCheckRuleComponent;
    @Resource
    private BaseActivityGoodsService activityGoodsService;
    @Resource
    private BaseActivityRuleService activityRuleService;
    @Resource
    private BaseActivityRecordService activityRecordService;
    @Resource
    private IScheduleFeignService scheduleFeignService;

    @Override
    public Wrapper<PageData<MerchantActivitySummaryPageResp>> pageSummary(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseMerchantActivityPage(loginUser, pageVO, pageVO.getOuterStatus(), pageVO.getInnerStatus());

        List<MerchantActivitySummaryPageResp> resultList = pageList.stream().map(e -> {
            MerchantActivitySummaryPageResp resp = new MerchantActivitySummaryPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setUpdate(MerchantActivityInnerStatusEnum.STOP.getCode().equals(e.getInnerStatus()));
            resp.setStop(MerchantActivityInnerStatusEnum.ONGOING.getCode().equals(e.getInnerStatus()));
            resp.setRestart(MerchantActivityInnerStatusEnum.STOP.getCode().equals(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<PageData<MerchantActivitySubmitExamPageResp>> pageByToBeSubmitExam(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseToBeSubmitActivityMerchantActivityPage(loginUser, pageVO);

        List<MerchantActivitySubmitExamPageResp> resultList = pageList.stream().map(e -> {
            MerchantActivitySubmitExamPageResp resp = new MerchantActivitySubmitExamPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setSubmit(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode().equals(e.getInnerStatus()));
            resp.setDelete(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode().equals(e.getInnerStatus()));
            List<Integer> updateStatusList = Arrays.asList(
                    MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode(),
                    MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_1.getCode(),
                    MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_2.getCode());
            resp.setUpdate(updateStatusList.contains(e.getInnerStatus()));

            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<PageData<MerchantActivityExamPageResp>> pageByExamineStep1(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseMerchantActivityPage(loginUser, pageVO, null, MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_1.getCode());
        List<MerchantActivityExamPageResp> resultList = pageList.stream().map(e -> {
            MerchantActivityExamPageResp resp = new MerchantActivityExamPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setExam(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_1.getCode().equals(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<PageData<MerchantActivityExamPageResp>> pageByExamineStep2(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseMerchantActivityPage(loginUser, pageVO, null, MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_2.getCode());
        List<MerchantActivityExamPageResp> resultList = pageList.stream().map(e -> {
            MerchantActivityExamPageResp resp = new MerchantActivityExamPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setExam(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_2.getCode().equals(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<PageData<MerchantActivitySubmitPageResp>> pageByToBeSubmit(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseMerchantActivityPage(loginUser, pageVO, null, MerchantActivityInnerStatusEnum.TO_BE_SUBMIT.getCode());
        List<MerchantActivitySubmitPageResp> resultList = pageList.stream().map(e -> {
            MerchantActivitySubmitPageResp resp = new MerchantActivitySubmitPageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setSubmit(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT.getCode().equals(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    @Override
    public Wrapper<PageData<MerchantActivityOnlinePageResp>> pageByToBeOnline(UserLoginCacheDTO loginUser, MerchantActivityCommonPageVO pageVO) {
        Page<MerchantActivityDO> pageList = baseMerchantActivityPage(loginUser, pageVO, null, MerchantActivityInnerStatusEnum.TO_BE_ONLINE.getCode());
        List<MerchantActivityOnlinePageResp> resultList = pageList.stream().map(e -> {
            MerchantActivityOnlinePageResp resp = new MerchantActivityOnlinePageResp();
            resp.setId(e.getId());
            resp.setActivityName(e.getActivityName());
            resp.setActivityType(e.getActivityType());
            resp.setActivityTypeName(ActivityTypeEnum.getMessage(e.getActivityType()));
            resp.setStartTime(e.getStartTime());
            resp.setEndTime(e.getEndTime());
            /*22.1.20版废弃
            resp.setOuterStatus(e.getOuterStatus());
            resp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(e.getOuterStatus()));*/
            resp.setInnerStatus(e.getInnerStatus());
            resp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(e.getInnerStatus()));
            resp.setOnline(MerchantActivityInnerStatusEnum.TO_BE_ONLINE.getCode().equals(e.getInnerStatus()));
            return resp;
        }).collect(Collectors.toList());

        return Wrapper.success(new PageData<>(pageList.getTotalElements(), resultList));
    }

    private MerchantActivityDetailResp builBaseDetail(MerchantActivityDO merchantActivityDO){
        MerchantActivityDetailResp detailResp = new MerchantActivityDetailResp();
        detailResp.setId(merchantActivityDO.getId());
        detailResp.setActivityType(merchantActivityDO.getActivityType());
        detailResp.setActivityTypeName(ActivityTypeEnum.getMessage(merchantActivityDO.getActivityType()));
        detailResp.setActivityName(merchantActivityDO.getActivityName());
        detailResp.setStartTime(merchantActivityDO.getStartTime());
        detailResp.setEndTime(merchantActivityDO.getEndTime());
        detailResp.setMemberName(merchantActivityDO.getMemberName());
        detailResp.setCreateTime(merchantActivityDO.getCreateTime());
        detailResp.setNewUser(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), SuitableFieldComponent.at_new_user_loc));
        detailResp.setOldUser(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), SuitableFieldComponent.at_ole_user_loc));
        detailResp.setNewMember(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), SuitableFieldComponent.at_new_member_loc));
        detailResp.setOldMember(SuitableFieldComponent.parseActivitySuitableUser(merchantActivityDO.getSuitableUser(), SuitableFieldComponent.at_old_member_loc));
        /*22.1.20版废弃
        detailResp.setOuterStatus(merchantActivityDO.getOuterStatus());
        detailResp.setOuterStatusName(MerchantActivityOuterStatusEnum.getMessage(merchantActivityDO.getOuterStatus()));
        detailResp.setOuterTaskId(merchantActivityDO.getOuterTaskId());
        detailResp.setOuterProcessKey(merchantActivityDO.getOuterProcessKey());
        detailResp.setOuterTaskStep(merchantActivityDO.getOuterTaskStep());*/
        detailResp.setInnerStatus(merchantActivityDO.getInnerStatus());
        detailResp.setInnerStatusName(MerchantActivityInnerStatusEnum.getMessage(merchantActivityDO.getInnerStatus()));
        detailResp.setInnerTaskId(merchantActivityDO.getInnerTaskId());
        detailResp.setInnerProcessKey(merchantActivityDO.getInnerProcessKey());
        detailResp.setInnerTaskStep(merchantActivityDO.getInnerTaskStep());
        detailResp.setActivityDefined(getActivityDefine(BelongTypeEnum.MERCHANT, merchantActivityDO.getActivityType(), merchantActivityDO.getActivityDefined()));
        return detailResp;
    }

    @Override
    public Wrapper<MerchantActivityDetailResp> detail(UserLoginCacheDTO loginUser, CommonIdReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        MerchantActivityDetailResp detailResp = builBaseDetail(merchantActivityDO);
        // 适用会员等级
        List<ActivityMemberLevelResp> memberLevelRespList = activityRuleService.listMemberLevel(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        detailResp.setMemberLevelList(memberLevelRespList);

        // 适用商城
        List<ActivityShopResp> shopRespList = activityRuleService.listShop(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        detailResp.setShopList(shopRespList);

        /*22.1.20版废弃
        // 查询外部工作流程
        List<TaskStepVO> outerTaskList = activityRecordService.listOuterTask(merchantActivityDO.getOuterProcessKey(), merchantActivityDO.getOuterTaskId());
        detailResp.setOuterTaskList(outerTaskList);


        // 封装外部流转记录
        List<ActivityOuterRecordResp> merchantOuterRecord = activityRecordService.getMerchantOuterRecord(loginUser, merchantActivityDO.getId());
        detailResp.setOuterRecordDOList(merchantOuterRecord);*/

        // 查询内部工作流程
        List<TaskStepVO> innerTaskList = activityRecordService.listInnerTask(merchantActivityDO.getInnerProcessKey(), merchantActivityDO.getInnerTaskId());
        detailResp.setInnerTaskList(innerTaskList);


        // 封装内部流转记录
        List<ActivityInnerRecordResp> merchantInnerRecord = activityRecordService.getMerchantInnerRecord(loginUser, merchantActivityDO.getId());
        detailResp.setInnerRecordDOList(merchantInnerRecord);

        return Wrapper.success(detailResp);
    }

    @Override
    public Wrapper<MerchantActivityDetailResp> getBaseDetail(UserLoginCacheDTO loginUser, CommonIdReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }
        return Wrapper.success(builBaseDetail(merchantActivityDO));
    }

    @Override
    public Wrapper<PageData<McActivityGoodsPageResp>> pageByActivityGoods(UserLoginCacheDTO loginUser, PfActivityGoodsPageReq pageVO) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(pageVO.getActivityId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        // 会员进行查询, 传入memberId, roleId
        return activityGoodsService.basePageMerchantActivityGoods(loginUser.getMemberId(), loginUser.getMemberRoleId(), pageVO.getActivityId(), pageVO.getCurrent(), pageVO.getPageSize(),merchantActivityDO.getActivityType());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> saveMerchantActivity(UserLoginCacheDTO loginUser, MerchantActivityAddReq request) {
        // 当前时间
        long currentTimeMillis = System.currentTimeMillis();
        // 判断活动时间
        Wrapper<Void> checkTimeWrapper = this.checkMerchantActivityTime(currentTimeMillis, request.getStartTime(), request.getEndTime());
        if (ResponseCode.SUCCESS.getCode() != checkTimeWrapper.getCode()) {
            return checkTimeWrapper;
        }

        // 校验各种活动类型对应的参数
        Wrapper<Void> checkActivityDefineWrapper = checkActivityDefine(request.getActivityType(), request.getActivityDefined());
        if (ResponseCode.SUCCESS.getCode() != checkActivityDefineWrapper.getCode()) {
            return checkActivityDefineWrapper;
        }

        // 校验适用商城
        activityRuleService.checkShop(request.getShopList());

        // 校验适用会员等级
        if (CommonBooleanEnum.YES.getCode().equals(request.getNewMember())
                || CommonBooleanEnum.YES.getCode().equals(request.getOldMember())) {
            if (!CollectionUtils.isEmpty(request.getMemberLevelList())) {
                activityRuleService.checkMemberLevel(request.getMemberLevelList());
            }
        }

        // 根据jsonObject, 转成对应活动定义BO对象
        ActivityDefinedBO activityDefinedBO = this.parseActivityDefinedBO(request.getActivityType(), request.getActivityDefined());

        // 校验活动商品
        Wrapper<Void> checkGoodsWrapper = activityGoodsService.checkActivityGoods(request.getActivityType(), activityDefinedBO, request.getProductList());
        if (ResponseCode.SUCCESS.getCode() != checkGoodsWrapper.getCode()) {
            return checkGoodsWrapper;
        }

        // 商品叠加活动校验
        Set<Long> filterSkuId = activityCheckRuleComponent.getFilterSkuIdByJsonObj(request.getActivityType(), request.getActivityDefined(), request.getStartTime(), request.getEndTime());
        if (request.getProductList().stream().anyMatch(e -> filterSkuId.contains(e.getSkuId()))) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_GOODS_SUPERPOSITION);
        }
        //查询商品skuList
        Map<Long, CommodityGoodsResponse> skuMap = activityGoodsService.getReqSkuMap(request.getProductList(),request.getActivityType(),loginUser.getMemberId(),loginUser.getMemberRoleId());
        if(skuMap==null){
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_GOODS_SKU_NOT_EXISTS_ERROR);
        }
        // 保存商家活动
        MerchantActivityDO merchantActivityDO = new MerchantActivityDO();
        merchantActivityDO.setActivityName(request.getActivityName());
        merchantActivityDO.setStartTime(request.getStartTime());
        merchantActivityDO.setEndTime(request.getEndTime());
        merchantActivityDO.setActivityType(request.getActivityType());
        merchantActivityDO.setMinType(this.getMinType(request.getActivityType(), request.getActivityDefined()));
        merchantActivityDO.setAllowCoupon(this.getAllowCoupon(request.getActivityType(), request.getActivityDefined()));
        merchantActivityDO.setActivityDefined(activityDefinedBO);
        merchantActivityDO.setSuitableUser(SuitableFieldComponent.generateActivitySuitableUser(request.getNewUser(), request.getOldUser(), request.getNewMember(), request.getOldMember()));
        merchantActivityDO.setCreateTime(System.currentTimeMillis());
        merchantActivityDO.setMemberId(loginUser.getMemberId());
        merchantActivityDO.setRoleId(loginUser.getMemberRoleId());
        merchantActivityDO.setMemberName(loginUser.getCompany());
        merchantActivityRepository.saveAndFlush(merchantActivityDO);

        // 适用会员等级
        activityRuleService.saveMemberLevel(BelongTypeEnum.MERCHANT, merchantActivityDO.getId(), request.getMemberLevelList());

        // 适用商城
        activityRuleService.saveShop(BelongTypeEnum.MERCHANT, merchantActivityDO.getId(), request.getShopList());

        // 保存商家活动商品
        activityGoodsService.saveMerchantActivityGoods(loginUser, merchantActivityDO.getId(), merchantActivityDO.getActivityType(), merchantActivityDO.getActivityDefined(), request.getProductList(),skuMap);

        /*22.1.20版废弃
        // 启动外部流程
        SimpleTaskCompleteVO outerTask = processFeignService.startSimpleProcess(loginUser.getMemberId(), loginUser.getMemberRoleId(), MarketingConstants.merchant_activity_outer_verify_key, merchantActivityDO.getId());
        merchantActivityDO.setOuterProcessKey(MarketingConstants.merchant_activity_outer_verify_key);
        merchantActivityDO.setOuterStatus(outerTask.getStatus());
        merchantActivityDO.setOuterTaskId(outerTask.getTaskId());
        merchantActivityDO.setOuterTaskStep(outerTask.getStep());*/

        // 启动内部流程
        SimpleTaskCompleteVO innerTask = processFeignService.startSimpleProcess(loginUser.getMemberId(), loginUser.getMemberRoleId(), MarketingConstants.merchant_activity_inner_key, merchantActivityDO.getId());
        merchantActivityDO.setInnerProcessKey(MarketingConstants.merchant_activity_inner_key);
        merchantActivityDO.setInnerStatus(innerTask.getStatus());
        merchantActivityDO.setInnerTaskId(innerTask.getTaskId());
        merchantActivityDO.setInnerTaskStep(innerTask.getStep());

        // 保存
        merchantActivityRepository.saveAndFlush(merchantActivityDO);

        // 活动结束定时任务
        scheduleFeignService.createMcActivityEndTask(loginUser, merchantActivityDO);

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> updateMerchantActivity(UserLoginCacheDTO loginUser, MerchantActivityUpdateReq request) {
        // 当前时间
        long currentTimeMillis = System.currentTimeMillis();

        // 判断活动时间
        Wrapper<Void> checkTimeWrapper = this.checkMerchantActivityTime(currentTimeMillis, request.getStartTime(), request.getEndTime());
        if (ResponseCode.SUCCESS.getCode() != checkTimeWrapper.getCode()) {
            return checkTimeWrapper;
        }

        // 校验适用商城
        activityRuleService.checkShop(request.getShopList());

        // 校验适用会员等级
        if (CommonBooleanEnum.YES.getCode().equals(request.getNewMember())
                || CommonBooleanEnum.YES.getCode().equals(request.getOldMember())) {
            if (!CollectionUtils.isEmpty(request.getMemberLevelList())) {
                activityRuleService.checkMemberLevel(request.getMemberLevelList());
            }
        }

        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_RECORDS_DONT_EXIST);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        // 内部状态判断,只能修改待提交,审核不通过(一级),审核不通过(二级), 已提交(平台审核不通过), 活动终止
        List<Integer> statusList = Arrays.asList(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode(),
                MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_1.getCode(),
                MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_2.getCode(),
                MerchantActivityInnerStatusEnum.STOP.getCode());
        boolean updateFlag = statusList.contains(merchantActivityDO.getInnerStatus());
        if (!updateFlag) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }

        // 校验各种活动类型对应的参数
        Wrapper<Void> checkActivityDefineWrapper = checkActivityDefine(merchantActivityDO.getActivityType(), request.getActivityDefined());
        if (ResponseCode.SUCCESS.getCode() != checkActivityDefineWrapper.getCode()) {
            return checkActivityDefineWrapper;
        }

        // 商品叠加活动校验
        Set<Long> filterSkuId = activityCheckRuleComponent.getFilterSkuIdByJsonObj(merchantActivityDO.getActivityType(), request.getActivityDefined(), request.getStartTime(), request.getEndTime());
        // 除去当前记录更新之前的数据
        Set<Long> goodsSkuIds = goodsRepository.findByBelongTypeAndActivityId(BelongTypeEnum.MERCHANT.getCode(), merchantActivityDO.getId()).stream().map(ActivityGoodsDO::getSkuId).collect(Collectors.toSet());
        filterSkuId.removeAll(goodsSkuIds);
        if (request.getProductList().stream().anyMatch(e -> filterSkuId.contains(e.getSkuId()))) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_GOODS_SUPERPOSITION);
        }

        // 根据jsonObject, 转成对应活动定义BO对象
        ActivityDefinedBO activityDefinedBO = this.parseActivityDefinedBO(merchantActivityDO.getActivityType(), request.getActivityDefined());

        // 校验活动商品
        Wrapper<Void> checkGoodsWrapper = activityGoodsService.checkActivityGoods(merchantActivityDO.getActivityType(), activityDefinedBO, request.getProductList());
        if (ResponseCode.SUCCESS.getCode() != checkGoodsWrapper.getCode()) {
            return checkGoodsWrapper;
        }

        // 内部拒绝状态
        List<Integer> rejectStatusList = Arrays.asList(MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_1.getCode(),
                MerchantActivityInnerStatusEnum.EXAMINE_NO_PASS_2.getCode());

        if (rejectStatusList.contains(merchantActivityDO.getInnerStatus())) {
            // 修改后为待提交
            merchantActivityDO.setInnerStatus(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode());
        }
        /*22.1.20版废弃
        else if(MerchantActivityOuterStatusEnum.NOT_PASSED.getCode().equals(merchantActivityDO.getOuterStatus())){
            // 修改后为外部 - 待提交, 内部 - 待提交审核
            merchantActivityDO.setOuterStatus(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode());
            merchantActivityDO.setInnerStatus(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode());
        }*/
        else if (MerchantActivityInnerStatusEnum.STOP.getCode().equals(merchantActivityDO.getInnerStatus())) {
            // 内部终止状态, 修改后重新走流程
            /*22.1.20版废弃
            // 启动外部流程
            SimpleTaskCompleteVO outerTask = processFeignService.startSimpleProcess(loginUser.getMemberId(), loginUser.getMemberRoleId(), MarketingConstants.merchant_activity_outer_verify_key, merchantActivityDO.getId());
            merchantActivityDO.setOuterProcessKey(MarketingConstants.merchant_activity_outer_verify_key);
            merchantActivityDO.setOuterStatus(outerTask.getStatus());
            merchantActivityDO.setOuterTaskId(outerTask.getTaskId());
            merchantActivityDO.setOuterTaskStep(outerTask.getStep());*/

            // 启动内部流程
            SimpleTaskCompleteVO innerTask = processFeignService.startSimpleProcess(loginUser.getMemberId(), loginUser.getMemberRoleId(), MarketingConstants.merchant_activity_inner_key, merchantActivityDO.getId());
            merchantActivityDO.setInnerProcessKey(MarketingConstants.merchant_activity_inner_key);
            merchantActivityDO.setInnerStatus(innerTask.getStatus());
            merchantActivityDO.setInnerTaskId(innerTask.getTaskId());
            merchantActivityDO.setInnerTaskStep(innerTask.getStep());
        }
        //查询商品skuList
        Map<Long, CommodityGoodsResponse> skuMap = activityGoodsService.getReqSkuMap(request.getProductList(),merchantActivityDO.getActivityType(),loginUser.getMemberId(),loginUser.getMemberRoleId());
        if(skuMap==null){
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_GOODS_SKU_NOT_EXISTS_ERROR);
        }
        // 保存商家活动
        merchantActivityDO.setActivityName(request.getActivityName());
        merchantActivityDO.setStartTime(request.getStartTime());
        merchantActivityDO.setEndTime(request.getEndTime());
        merchantActivityDO.setSuitableUser(SuitableFieldComponent.generateActivitySuitableUser(request.getNewUser(), request.getOldUser(), request.getNewMember(), request.getOldMember()));
        merchantActivityDO.setActivityDefined(activityDefinedBO);
        merchantActivityDO.setMinType(this.getMinType(merchantActivityDO.getActivityType(), request.getActivityDefined()));
        merchantActivityDO.setAllowCoupon(this.getAllowCoupon(merchantActivityDO.getActivityType(), request.getActivityDefined()));
        merchantActivityRepository.saveAndFlush(merchantActivityDO);

        // 适用会员等级
        activityRuleService.deleteMemberLevel(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        activityRuleService.saveMemberLevel(BelongTypeEnum.MERCHANT, merchantActivityDO.getId(), request.getMemberLevelList());

        // 适用商城
        activityRuleService.deleteShop(BelongTypeEnum.MERCHANT, merchantActivityDO.getId());
        activityRuleService.saveShop(BelongTypeEnum.MERCHANT, merchantActivityDO.getId(), request.getShopList());

        // 保存商家活动商品
        activityGoodsService.deleteActivityGoods(merchantActivityDO);
        activityGoodsService.saveMerchantActivityGoods(loginUser, merchantActivityDO.getId(), merchantActivityDO.getActivityType(), merchantActivityDO.getActivityDefined(), request.getProductList(),skuMap);

        return Wrapper.success();
    }

    @Override
    public Wrapper<FilterSkuIdResponse> getFilterSkuId(UserLoginCacheDTO loginUser, FilterSkuIdRequest request) {
        Set<Long> skuIds = activityCheckRuleComponent.getFilterSkuIdByJsonObj(request.getActivityType(), request.getActivityDefined(), request.getStartTime(), request.getEndTime());

        FilterSkuIdResponse response = new FilterSkuIdResponse();
        response.setFilterSkuId(skuIds);
        return Wrapper.success(response);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> batchDelete(UserLoginCacheDTO loginUser, List<Long> idList) {
        List<MerchantActivityDO> activityDOS = merchantActivityRepository.findAllById(idList);
        if (CollectionUtils.isEmpty(activityDOS)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : activityDOS) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            /*22.1.20版废弃
            if (!MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode().equals(merchantActivityDO.getOuterStatus())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }*/
            if (!MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode().equals(merchantActivityDO.getInnerStatus())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
        }

        // 删除活动
        merchantActivityRepository.deleteAll(activityDOS);
        // 删除会员等级
        memberLevelRepository.deleteByBelongTypeAndActivityIdIn(BelongTypeEnum.MERCHANT.getCode(), idList);
        // 删除商城
        shopRepository.deleteByBelongTypeAndActivityIdIn(BelongTypeEnum.MERCHANT.getCode(), idList);
        // 删除商品
        for (MerchantActivityDO activityDO : activityDOS) {
            activityGoodsService.deleteActivityGoods(activityDO);
        }

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> submitExamine(UserLoginCacheDTO loginUser, List<Long> idList) {
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(idList);
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        // 批量审核状态为待审核的数据
        List<MerchantActivityDO> updateList = new ArrayList<>();
        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT_EXAM.getCode())) {
                continue;
            }
            updateList.add(merchantActivityDO);
        }

        for (MerchantActivityDO merchantActivityDO : updateList) {
            // 执行内部流转
            TaskExecuteVO executeVO = new TaskExecuteVO();
            executeVO.setProcessKey(merchantActivityDO.getInnerProcessKey());
            executeVO.setMemberId(loginUser.getMemberId());
            executeVO.setRoleId(loginUser.getMemberRoleId());
            executeVO.setDataId(merchantActivityDO.getId());
            executeVO.setAgree(ProcessTaskStatusEnum.GOTO_NEXT_STEP.getCode());
            executeVO.setTaskId(merchantActivityDO.getInnerTaskId());
            Wrapper<SimpleTaskCompleteVO> result = processFeignService.completeSimpleTask(executeVO);
            if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
                throw new BusinessException(ResponseCode.SERVICE_WORKFLOW_ERROR);
            }
            merchantActivityDO.setInnerTaskId(result.getData().getTaskId());
            merchantActivityDO.setInnerTaskStep(result.getData().getStep());
            merchantActivityDO.setInnerStatus(result.getData().getStatus());
            merchantActivityRepository.saveAndFlush(merchantActivityDO);

            // 内部流转记录
            activityRecordService.saveMerchantActivityInnerRecord(loginUser, merchantActivityDO.getId(),
                    result.getData().getStatus(), MerchantActivityInnerStatusEnum.getMessage(result.getData().getStatus()),
                    ActivityStrOperateEnum.mc_inner_submit_audit.getMessage(), "");
        }

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> examineStep1Update(UserLoginCacheDTO loginUser, ActivityExamineReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }
        /*22.1.20版废弃
        if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }*/

        if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_1.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }

        this.baseExamineStep1Update(loginUser, Collections.singletonList(merchantActivityDO), request.getIsPass(), request.getOpinion());
        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> batchExamineStep1Update(UserLoginCacheDTO loginUser, CommonIdsReq request) {
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(request.getIds());
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            /*22.1.20版废弃
            if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }*/
            if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_1.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
        }

        this.baseExamineStep1Update(loginUser, merchantActivityDOList, ProcessTaskStatusEnum.AGREE.getCode(), "");
        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> examineStep2Update(UserLoginCacheDTO loginUser, ActivityExamineReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }
        /*22.1.20版废弃
        if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }*/

        if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_2.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
        }

        this.baseExamineStep2Update(loginUser, Collections.singletonList(merchantActivityDO), request.getIsPass(), request.getOpinion());
        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> batchExamineStep2Update(UserLoginCacheDTO loginUser, CommonIdsReq request) {
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(request.getIds());
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            /*22.1.20版废弃
            if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }*/
            if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_EXAMINE_2.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
        }

        this.baseExamineStep2Update(loginUser, merchantActivityDOList, ProcessTaskStatusEnum.AGREE.getCode(), "");
        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> submit(UserLoginCacheDTO loginUser, List<Long> idList) {
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(idList);
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            /*22.1.20版废弃
            if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_SUBMIT.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }*/
            if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_SUBMIT.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            /*22.1.20版废弃
            // 执行外部流转
            TaskExecuteVO taskStartVO = new TaskExecuteVO();
            taskStartVO.setProcessKey(merchantActivityDO.getOuterProcessKey());
            taskStartVO.setMemberId(loginUser.getMemberId());
            taskStartVO.setRoleId(loginUser.getMemberRoleId());
            taskStartVO.setDataId(merchantActivityDO.getId());
            taskStartVO.setAgree(ProcessTaskStatusEnum.GOTO_NEXT_STEP.getCode());
            taskStartVO.setTaskId(merchantActivityDO.getOuterTaskId());
            Wrapper<SimpleTaskCompleteVO> outerResult = processFeignService.completeSimpleTask(taskStartVO);
            if (outerResult.getCode() != ResponseCode.SUCCESS.getCode()) {
                throw new BusinessException(ResponseCode.SERVICE_WORKFLOW_ERROR);
            }
            merchantActivityDO.setOuterTaskId(outerResult.getData().getTaskId());
            merchantActivityDO.setOuterTaskStep(outerResult.getData().getStep());
            merchantActivityDO.setOuterStatus(outerResult.getData().getStatus());
            merchantActivityRepository.saveAndFlush(merchantActivityDO);

            //外部流转记录
            activityRecordService.saveMerchantActivityOuterRecord(loginUser, merchantActivityDO.getId(),
                    outerResult.getData().getStatus(), outerResult.getData().getStatusDesc(),
                    outerResult.getData().getProperties().getOrDefault("oper", ""), "");*/

            // 执行内部流转
            TaskExecuteVO executeVO = new TaskExecuteVO();
            executeVO.setProcessKey(merchantActivityDO.getInnerProcessKey());
            executeVO.setMemberId(loginUser.getMemberId());
            executeVO.setRoleId(loginUser.getMemberRoleId());
            executeVO.setDataId(merchantActivityDO.getId());
            executeVO.setAgree(ProcessTaskStatusEnum.GOTO_NEXT_STEP.getCode());
            executeVO.setTaskId(merchantActivityDO.getInnerTaskId());
            Wrapper<SimpleTaskCompleteVO> result = processFeignService.completeSimpleTask(executeVO);
            if (result.getCode() != ResponseCode.SUCCESS.getCode()) {
                throw new BusinessException(ResponseCode.SERVICE_WORKFLOW_ERROR);
            }
            merchantActivityDO.setInnerTaskId(result.getData().getTaskId());
            merchantActivityDO.setInnerTaskStep(result.getData().getStep());
            merchantActivityDO.setInnerStatus(result.getData().getStatus());
            merchantActivityRepository.saveAndFlush(merchantActivityDO);

            // 内部流转记录
            activityRecordService.saveMerchantActivityInnerRecord(loginUser, merchantActivityDO.getId(),
                    result.getData().getStatus(), MerchantActivityInnerStatusEnum.getMessage(result.getData().getStatus()),
                    ActivityStrOperateEnum.mc_inner_submit.getMessage(), "");
        }

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> onlineUpdate(UserLoginCacheDTO loginUser, List<Long> idList) {
        long currentTimeMillis = System.currentTimeMillis();
        List<MerchantActivityDO> merchantActivityDOList = merchantActivityRepository.findAllById(idList);
        if (CollectionUtils.isEmpty(merchantActivityDOList)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
            }
            /*22.1.20版废弃
            if (!merchantActivityDO.getOuterStatus().equals(MerchantActivityOuterStatusEnum.TO_BE_ONLINE.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }*/
            if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.TO_BE_ONLINE.getCode())) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_CURRENT_STATUS_NOT_ALLOW_UPDATE);
            }
            // 过期不允许上线
            if (currentTimeMillis >= merchantActivityDO.getEndTime()) {
                return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_INVALID_NOT_ALLOW_UPDATE);
            }
        }

        for (MerchantActivityDO merchantActivityDO : merchantActivityDOList) {
            /*22.1.20版废弃
            // 执行外部流转
            TaskExecuteVO taskStartVO = new TaskExecuteVO();
            taskStartVO.setProcessKey(merchantActivityDO.getOuterProcessKey());
            taskStartVO.setMemberId(loginUser.getMemberId());
            taskStartVO.setRoleId(loginUser.getMemberRoleId());
            taskStartVO.setDataId(merchantActivityDO.getId());
            taskStartVO.setAgree(ProcessTaskStatusEnum.GOTO_NEXT_STEP.getCode());
            taskStartVO.setTaskId(merchantActivityDO.getOuterTaskId());
            Wrapper<SimpleTaskCompleteVO> outerResult = processFeignService.completeSimpleTask(taskStartVO);
            if (outerResult.getCode() != ResponseCode.SUCCESS.getCode()) {
                throw new BusinessException(ResponseCode.SERVICE_WORKFLOW_ERROR);
            }
            merchantActivityDO.setOuterTaskId(outerResult.getData().getTaskId());
            merchantActivityDO.setOuterTaskStep(outerResult.getData().getStep());
            merchantActivityDO.setOuterStatus(outerResult.getData().getStatus());
            merchantActivityRepository.saveAndFlush(merchantActivityDO);

            //外部流转记录
            activityRecordService.saveMerchantActivityOuterRecord(loginUser, merchantActivityDO.getId(),
                    outerResult.getData().getStatus(), outerResult.getData().getStatusDesc(),
                    outerResult.getData().getProperties().getOrDefault("oper", ""), "");*/

            // 执行内部流转
            TaskExecuteVO executeVO = new TaskExecuteVO();
            executeVO.setProcessKey(merchantActivityDO.getInnerProcessKey());
            executeVO.setMemberId(loginUser.getMemberId());
            executeVO.setRoleId(loginUser.getMemberRoleId());
            executeVO.setDataId(merchantActivityDO.getId());
            executeVO.setAgree(ProcessTaskStatusEnum.GOTO_NEXT_STEP.getCode());
            executeVO.setTaskId(merchantActivityDO.getInnerTaskId());
            Wrapper<SimpleTaskCompleteVO> innerResult = processFeignService.completeSimpleTask(executeVO);
            if (innerResult.getCode() != ResponseCode.SUCCESS.getCode()) {
                throw new BusinessException(ResponseCode.SERVICE_WORKFLOW_ERROR);
            }
            merchantActivityDO.setInnerTaskId(innerResult.getData().getTaskId());
            merchantActivityDO.setInnerTaskStep(innerResult.getData().getStep());
            merchantActivityDO.setInnerStatus(innerResult.getData().getStatus());
            merchantActivityRepository.saveAndFlush(merchantActivityDO);

            // 内部流转记录
            activityRecordService.saveMerchantActivityInnerRecord(loginUser, merchantActivityDO.getId(),
                    innerResult.getData().getStatus(), MerchantActivityInnerStatusEnum.getMessage(innerResult.getData().getStatus()),
                    ActivityStrOperateEnum.mc_inner_online.getMessage(), "");
        }

        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> stop(UserLoginCacheDTO loginUser, ActivityActionReq request) {
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.ONGOING.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_ALLOW_UPDATE);
        }

        // 更新状态
        /*22.1.20版废弃
        Integer outStatus = MerchantActivityOuterStatusEnum.STOP.getCode();
        merchantActivityDO.setOuterStatus(outStatus);
        merchantActivityDO.setOuterTaskStep(0);*/
        Integer innerStatus = MerchantActivityInnerStatusEnum.STOP.getCode();
        merchantActivityDO.setInnerStatus(innerStatus);
        merchantActivityDO.setInnerTaskStep(0);
        merchantActivityRepository.saveAndFlush(merchantActivityDO);

        // 外部流转记录
        //22.1.20版废弃 activityRecordService.saveMerchantActivityOuterRecord(loginUser, merchantActivityDO.getId(), outStatus, MerchantActivityOuterStatusEnum.getMessage(outStatus), "终止", request.getReason());

        // 内部流转记录
        activityRecordService.saveMerchantActivityInnerRecord(loginUser, merchantActivityDO.getId(), innerStatus, MerchantActivityInnerStatusEnum.getMessage(innerStatus), ActivityStrOperateEnum.mc_inner_stop.getMessage(), request.getReason());
        return Wrapper.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Wrapper<Void> restart(UserLoginCacheDTO loginUser, ActivityActionReq request) {
        long currentTimeMillis = System.currentTimeMillis();
        MerchantActivityDO merchantActivityDO = merchantActivityRepository.findById(request.getId()).orElse(null);
        if (Objects.isNull(merchantActivityDO)) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }

        if (!isBelongCurrentMember(loginUser, merchantActivityDO.getMemberId(), merchantActivityDO.getRoleId())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_EXISTS);
        }
        /*22.1.20版废弃
        if (!merchantActivityDO.getInnerStatus().equals(MerchantActivityInnerStatusEnum.STOP.getCode())) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_NOT_ALLOW_UPDATE);
        }*/

        // 过期不允许重新启动
        if (currentTimeMillis >= merchantActivityDO.getEndTime()) {
            return Wrapper.fail(ResponseCode.MARKETING_ACTIVITY_INVALID_NOT_ALLOW_UPDATE);
        }

        // 更新状态
        /*22.1.20版废弃
        Integer outStatus = MerchantActivityOuterStatusEnum.ONGOING.getCode();
        merchantActivityDO.setOuterStatus(outStatus);
        merchantActivityDO.setOuterTaskStep(0);*/
        Integer innerStatus = MerchantActivityInnerStatusEnum.ONGOING.getCode();
        merchantActivityDO.setInnerStatus(innerStatus);
        merchantActivityDO.setInnerTaskStep(0);
        merchantActivityRepository.saveAndFlush(merchantActivityDO);

        // 外部流转记录
        //22.1.20版废弃 activityRecordService.saveMerchantActivityOuterRecord(loginUser, merchantActivityDO.getId(), outStatus, MerchantActivityOuterStatusEnum.getMessage(outStatus), "重新启动", request.getReason());

        // 内部流转记录
        activityRecordService.saveMerchantActivityInnerRecord(loginUser, merchantActivityDO.getId(), innerStatus, MerchantActivityInnerStatusEnum.getMessage(innerStatus), ActivityStrOperateEnum.mc_inner_restart.getMessage(), request.getReason());
        return Wrapper.success();
    }
}
